package mysql

import (
	"home-view/homerpc/homerpc"

	"gorm.io/gorm"
)

type Class struct {
	gorm.Model
	ClassName string  `gorm:"type:varchar(30);not null;comment:'课程名称'"`
	ClassType string  `gorm:"type:varchar(30);not null;comment:'课程分类'"`
	Price     float32 `gorm:"type:decimal(10,2);not null;comment:'课程价格'"`
	Content   string  `gorm:"type:varchar(200);not null;comment:'课程描述'"`
	UserId    int64   `gorm:"type:int(11);not null;comment:'用户ID'"`
}

func (c *Class) ClassAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Class) ClassList(db *gorm.DB, name string, in *homerpc.ClassListRequest, lists []*homerpc.ClassList) ([]*homerpc.ClassList, error) {
	/*
		SELECT
		classes.id as class_id,classes.class_name,classes.class_type,classes.price,classes.content,users.nick_name
		FROM
		classes
		LEFT JOIN users ON users.id=classes.user_id
	*/
	err := db.Debug().Model(c).Select("classes.id as class_id,classes.class_name,classes.class_type,classes.price,classes.content,users.nick_name").
		Joins("LEFT JOIN users ON users.id=classes.user_id").
		Where("classes.class_name like ?", "%"+name+"%").
		Order("classes.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&lists).Error
	return lists, err
}
